using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using UnityEngine;

public class VLifeCore_Dog : VLifeCore
{
    public VActBehaviour behaviour;
    private LLMIP_DogTalk curHandler;

    public Dictionary<string, TriggerObserver> actions;
    public Transform farStone;
    public void Start()
    {
        behaviour = gameObject.UniqueComponent<VActBehaviour>();
        actions = new Dictionary<string, TriggerObserver>();
        var mvm = GetComponent<VActProcessor>().mechine;
//         actions["FreeToPlay"] = new TriggerObserver()
//         {
//             start = () =>
//             {
//                 print("那我玩去啦");
//                 if (mvm.RandomSampleOnNavMesh(out var point))
//                 {
//                     behaviour.WalkToPoint(point);
//                 }
//                 else
//                 {
//                     throw new System.Exception("找不到导航点,检查网格和层级!");
//                 }
// 
//             }
//         };
// 
//         actions["GoToSecondFloor"] = new TriggerObserver()
//         {
//             start = () =>
//             {
//                 var pos = GameObject.Find("SecondFloor");
//                 behaviour.WalkToPoint(pos.transform.position);
//             }
//         };
//         actions["GoToGate"] = new TriggerObserver()
//         {
//             start = () =>
//             {
//                 var pos = GameObject.Find("Gate");
//                 behaviour.WalkToPoint(pos.transform.position);
//             }
//         };
// 
//         actions["ComeBack"] = new TriggerObserver()
//         {
//             start = behaviour.ComeBack
//         };
// 
//         actions["Happy"] = new TriggerObserver()
//         {
//             start = () => behaviour.Show("Happy", true)
//         };
// 
//         actions["SitDown"] = new TriggerObserver()
//         {
//             start = () => behaviour.Show("SitDown", false)
//         };
// 
//         actions["StandUp"] = new TriggerObserver()
//         {
//             start = () => behaviour.Show("StandUp", false)
//         };
// 
//         actions["UseSkill"] = new TriggerObserver()
//         {
//             start = () =>
//             {
//                 //参数解析
//                 print("Process UseSKill");
//                 var skillname = useSkillPack.skillName;
//                 PPokemonInfo.instanceTable.TryGetValue(useSkillPack.instanceID,out var targetPokemon);
//                 //var targetPokemon = PPokemonInfo.instanceTable[useSkillPack.instanceID];
//                 if (skillname == "Bite")
//                 {
//                     //单列了
//                     VAudio.LoadPlayAudioAsync(PAudio.GetPokemonAudioPath(262, PAudio.PokemonVoiceType.Atk));
//                     var oripos = transform.position;
//                     behaviour.WalkToPoint(targetPokemon.transform,2f, () =>
//                     {
//                         behaviour.input.ForceDoAct("Bite");//做完之后应该走回来的
//                         //伪造一下
//                         0.75f.Delay(targetPokemon.behaviour.Hit);//播放走路动画);
//                         var loader = GetComponent<VActSkill_Loader>();
//                         loader.overrideSkill.onSkillComplete+=() =>
//                         {
//                             behaviour.WalkToPoint(oripos,()=>behaviour.TurnToPoint(targetPokemon.transform.position));
//                         };
//                     });
//                     return;
//                 }
//                 else if (skillname == "FreeMove")
//                 {
//                     //暂时挪用一下
//                     var ActBehaviours = GameObject.FindGameObjectsWithTag("Pokemon").ToList().Select(x => x.GetComponent<VActBehaviour>()).ToList();
//                     foreach (var actBehaviour in ActBehaviours)
//                     {
//                         if (actBehaviour.SampleRandomPoint(out var pos))
//                             actBehaviour.WalkToPoint(pos);
//                     }
//                     return;
//                 }
//                 else if (skillname == "LookAtMe")
//                 {
//                     //暂时挪用一下
//                     var ActBehaviours = GameObject.FindGameObjectsWithTag("Pokemon").ToList().Select(x => x.GetComponent<VActBehaviour>()).ToList();
//                     foreach (var actBehaviour in ActBehaviours)
//                     {
//                         actBehaviour.TurnToPoint(transform.position);                            
//                     }
//                     return;
//                 }else if (skillname == "GoToStone")
//                 {
//                     behaviour.WalkToPoint(farStone, 1f);
//                     VAudio.LoadPlayAudioAsync(PAudio.GetPokemonAudioPath(262, PAudio.PokemonVoiceType.Ex));
//                     return;
//                 }else if (skillname == "CallBack")
//                 {
//                     VAudio.LoadPlayAudioAsync(PAudio.GetPokemonAudioPath(262, PAudio.PokemonVoiceType.Glad));
//                     behaviour.WalkToPoint(Camera.main.transform,6f);
//                     return;
//                 }
//                 behaviour.input.ForceDoAct(skillname);
//                 behaviour.processor.data.cache[skillname] = new VActSkillParam_Target()
//                 {
//                     targetTransform = targetPokemon.transform,
//                     targetProcessor = targetPokemon.processor,
//                 };
// 
// 
//             }
//         };
    }
    public LLMIP_DogBattle.JsonPack useSkillPack;
    public LLMIP_DogBattle battleHandler;
    public void UserBattle(string context)
    {
        //搜集信息
        var pokemons = GameObject.FindGameObjectsWithTag("Pokemon").ToList().Select(x=>x.GetComponent<PPokemonInfo>());
        var infos = pokemons.OrderBy((x) => (x.transform.position - transform.position).magnitude).ToList();
        var enemyInfo = infos.ToLLMString((x) =>
        $"{x.pname}: instanceid={x.iid},distace:{(x.transform.position - transform.position).magnitude}"
        ,"敌方信息"
        );
        var skillInfo = ",可用Skill:Idle,Bite,DarkBullet,FreeMove,LookAtMe,GoToStone,CallBack";
        battleHandler = new LLMIP_DogBattle(enemyInfo+skillInfo)
        {
            interactContext = context,
        };

        battleHandler.onResponse += (str) =>
        {
            battleHandler.TryDeJson(out useSkillPack);
            actions["UseSkill"].Subscribe();
        };

        battleHandler.Generate();
    }



}

public class LLMIP_DogBattle : LLMInteractPairsBase
{
    // 这里本应二次分流的交互链，时间所限直接开全局视野了
    // public override string prompt => prompt;
    // public override string promptContextHeader =>"本次指示:" ;
    public override string prompt => _prompt;
    private string _prompt;
    public LLMIP_DogBattle(string selectionSet)
    {
        _prompt = "你是一只宝可梦，现在正在战斗中，根据训练家的命令做出最符合语义的选择，遵循如下规则：" +
            "1.只能够使用下列定义的行为填充Json类：" + selectionSet;
    }
    public override IJsonable JsonableObject => new JsonPack();
    public class JsonPack : IJsonable
    {
        public int instanceID;
        public string skillName;
    }
}


